package com.sso.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sso.entity.Maps;
import com.sso.entity.Task;
import com.sso.feign.UserFeignService;
import com.sso.filter.R;
import com.sso.form.JsonDto;
import com.sso.form.JsonDto2;
import com.sso.mapper.MapsMapper;
import com.sso.service.MapsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sso.service.TaskService;
import com.sso.utils.CreateJsonUtil;
import com.sso.utils.MyProperty;
import com.sso.utils.ThreadPoolUtil;
import com.sso.utils.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.FutureTask;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xia
 * @since 2022-07-23
 */
@Service
public class MapsServiceImpl extends ServiceImpl<MapsMapper, Maps> implements MapsService {
    @Autowired
    private MapsService mapsService;
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private MyProperty myProperty;
    @Autowired
    private UserImpl userImpl;
    @Override
    public R gapClassification(Integer pictureId, String path) {
        // 判断是否为游客操作，传值游客id，否则不传
        List<Maps> maps1 = mapsService.list(new QueryWrapper<Maps>().lambda()
                .eq(Maps::getSampleId,pictureId));
        Integer userId=userImpl.get().getId();
        int scale=maps1.get(0).getSize();
        // 开始调用算法，修改任务状态，未运行
        Task task = new Task();
        task.setPictureId(pictureId);
        task.setUserId(userId);
        task.setTaskType(1);
        task.setTaskStatus(0);
        taskService.save(task);
        int insert = task.getId();
        System.out.println("task主键" + task.getId());
        // 创建Json配置文件，有任务排队的时候有问题，可能被覆盖
        JsonDto jsonDto = new JsonDto();
        jsonDto.setBlc(scale).setMaps_id(maps1.get(0).getId()).setPicture_id(pictureId).setPath(path).setPersonal_id(userId);
        CreateJsonUtil.createJsonFile1(jsonDto, myProperty.getJsonPath(), "config");
        String py = "predict.py";
        ThreadUtil threadUtil = new ThreadUtil();
        threadUtil.initialize(task.getId(), py, Math.toIntExact(scale), path);
        FutureTask futureTask = new FutureTask(threadUtil);
        try {
            ThreadPoolUtil.threadPool.submit(futureTask);
            System.out.println("******************");
        } catch (Exception e) {
            // 修改任务状态，异常结束
            log.error("异常结束");
            e.printStackTrace();
            task.setTaskStatus(3);
            taskService.updateById(task);
        }
        return R.ok("开始调用算法").put("TaskId",task.getId());
    }
    @Override
    public R gapClassification1(Integer pictureId, String path){
        // 开始调用算法，修改任务状态，未运行
        // 判断是否为游客操作，传值游客id，否则不传
        List<Maps> maps1 = mapsService.list(new QueryWrapper<Maps>().lambda()
                .eq(Maps::getSampleId,pictureId));
        Integer userId=userImpl.get().getId();
        Task task = new Task();
        task.setPictureId(pictureId);
        task.setUserId(userId);
        task.setTaskType(2);
        task.setTaskStatus(0);
        taskService.save(task);
        int insert = task.getId();
        System.out.println("task主键" + task.getId());
        // 创建Json配置文件，有任务排队的时候有问题，可能被覆盖
        JsonDto2 jsonDto2 = new JsonDto2();
        jsonDto2.setMaps_id(maps1.get(0).getId()).setPicture_id(pictureId).setPath(path).setPersonal_id(userId);
        CreateJsonUtil.createJsonFile(jsonDto2, myProperty.getJsonPath(), "config");
        String py = "predict2.py";
        ThreadUtil threadUtil = new ThreadUtil();
        threadUtil.initialize(task.getId(), py, 1, path);
        FutureTask futureTask = new FutureTask(threadUtil);
        try {
            ThreadPoolUtil.threadPool.submit(futureTask);
            System.out.println("******************");
        } catch (Exception e) {
            // 修改任务状态，异常结束
            log.error("异常结束");
            e.printStackTrace();
            task.setTaskStatus(3);
            taskService.updateById(task);
        }
        return R.ok("开始调用算法").put("TaskId",task.getId());
    }
}
